<? include ("../init.php") ?>
<? include ("../header.php") ?>
<? include ("../menu.php") ?>
<? include ("submenu.php") ?>
<?php
  if(isset($_GET['aid'])){
    $project_id = $_GET['aid'];
  }else{
    $project_id = $_SESSION['ADDPROJECTID'];
  }

  $sql = "SELECT * FROM projects WHERE project_id = '".$project_id."'";
  $line = $db->queryUniqueObject($sql);
  
  $sql = 'SELECT * FROM project_cat ORDER BY project_cat_name';
  $rss = $db->query($sql);
  $select_cat = '<select name = "catalogue" class = "nput size1" >';
  while ($line2 = $db->fetchNextObject($rss)){
    if($line2->project_cat_id==$line->project_cat_id){
      $selected = 'selected="selected"';
    }else{
      $selected = '';
    }
    $select_cat .= "<option ".$selected." value = '".$line2->project_cat_id."'>".$line2->project_cat_name."</option>";
  }
  $select_cat .= "</select>";
  
  $sql = 'SELECT * FROM project_img WHERE project_id = '.$project_id.' ORDER BY img_position';
  $rss = $db->query($sql);
  $i = 1;
  $image_gallery = '';
  $lastPos = '';
  
  while ($line2 = $db->fetchNextObject($rss)){
    $image_gallery .= <<<image_gallery
      <li id="slideOrder_{$i}">
        <div>
          <img src="../images/ajax-loader.gif" class="ajax_load_img delete_indicator"/>
          <img src="../../images/projects/{$project_id}/{$line2->project_img_path}" class="imgGallThumb" />
          <p>
            <a href="javascript:void(0);" id="{$line2->project_img_id}" class="aLink deleteImg">Delete</a>
          </p>
        </div>
        <span class="hiddenEle" id="imgOderNew"><p>{$line2->project_img_id}</p></span>
      </li>
image_gallery;
    ++$i;
    $lastPos = $line2->img_position;
  }
?>
<div id = "body">

<h1><?_l('edit')?> <?_l('project')?></h1>

<!--<a href = "javascript:void(0)" onclick = "uploadProjectImg(<?php echo $project_id; ?>)" class = "add"><img src = "<?=ADMIN?>/images/icon-add.png">&nbsp;&nbsp;&nbsp;<?_l('upload-more-images')?></a>-->

<div class = "clear"></div>

<form method="post" action="update.php" target="ajaxIfrm"  class = "form" enctype="multipart/form-data">
  <input name="project_id" type="hidden" value="<?=$project_id?>"/>
  <input name="old_img" type="hidden" value="<?=$line->project_img?>" />
  <input name="old_cert" type="hidden" value="<?=$line->project_cert?>" />
  
  <div class = "field"><label><?_l('catalogue')?></label><?php echo $select_cat; ?></div>
  <div class = "field"><label><?_l('name')?></label><input type = "text" name = "name" class = "input size1" value="<?=$line->project_title?>" ></div>
  <div class = "field"><label><?_l('date')?></label><input type = "text" name = "date" class = "input size1" id="datepicker"value="<?=$line->project_date?>" ></div>
  <div class = "field"><label><?_l('area')?></label><input type = "text" name = "area" class = "input size1" value="<?=$line->project_area?>" ></div>
  <div class = "field"><label><?_l('owner')?></label><input type = "text" name = "owner" class = "input size1" value="<?=$line->project_owner?>" ></div>
  <!--<div class = "field"><label><?_l('complete-year')?></label><?php echo $select_year; ?></div>-->
  <div class = "field"><label><?_l('complete-year')?></label><input type = "text" name = "year" class = "input size1" value="<?=$line->project_complete_year?>" ></div>
  <div class = "field"><label><?_l('value')?></label><input type = "text" name = "value" class = "input size1" value="<?=$line->project_value?>" ></div>
  
  <div class = "field"><label><?_l('image')?></label><input type = "file" name = "project_img" class = "input size1" ></div>
  <?php
    if($line->project_img!='' && file_exists('../../images/projects/'.$project_id.'/'.$line->project_img)){
      echo '<div class = "field"><label>&nbsp;</label><img src="../../images/projects/'.$project_id.'/'.$line->project_img.'" /></div>';
    }
  ?>
  
  <div class = "field"><label><?_l('certificate')?></label><input type = "file" name = "cert" class = "input size1" ></div>
  <?php
    if($line->project_cert!='' && file_exists('../../images/projects/'.$project_id.'/'.$line->project_cert)){
      echo '<div class = "field"><label>&nbsp;</label><a href="../../images/projects/'.$project_id.'/'.$line->project_cert.'" title="'.__l("certificate").'">'.$line->project_cert.'</a>&nbsp;&nbsp;&nbsp;<img src="../images/del.png" alt"delete file" id="delete_cert" /></div>';
    }
  ?>
  
  <br />
  
  <div class = "field">
    <label><?_l('gallery')?></label>
    <input type="file" id="file_upload" name="file_upload" />
  </div>
  
  <div class = "field">
    <div id="tabs-1" class="imagesOrder">
      <!--<form name="imagesOrderFrm" id="imagesOrderFrm">-->
        <ul class="imagesOrderlist" id="list1">
        <?php echo $image_gallery; ?>
        </ul>
        <div class = "clear"></div>
      <!--</form>-->
      <input name="list1SortOrder" type="hidden" />
    </div>
  </div>
    ลากรูปภาพเพื่อจัดตำแหน่ง จากนั้นกดปุ่มอัพเดต
  <br />
  <div class = "field"><label></label>
    <input type="submit" name="save" value = "<?_l('update')?>" class = "btn" />
    <input onclick = "javascript:history.back(1)" type = "button" value="<?_l('cancel')?>" class = "btn" />
  </div>
  
  <div class = "clear"></div>
</form>

</div>
<iframe class="hiddenEle" name="ajaxIfrm"></iframe>

<style>
  .form label {
    width:80px;
  }
</style>
<script type="text/javascript" src="../js/jquery.dragsort-0.3.10.js"></script>
<link href="../js/jquery.uploadify/uploadify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery.uploadify/swfobject.js"></script>
<script type="text/javascript" src="../js/jquery.uploadify/jquery.uploadify.v2.1.4.min.js"></script>
    <script type="text/javascript">
	$(document).ready(function(){
	    $('#datepicker').datepicker({
	      dateFormat: 'yy-mm-dd',
	      changeMonth: true,
	      changeYear: true
	    });
	    
	    $('.deleteImg').click(function(){
		
		var imgEle = $(this);
		var id = imgEle.attr('id');
		
		if(confirm('Sure?'))
		{
		    imgEle.parents("li").find('.delete_indicator').css('display','inline');
		    $.post('del.php','imgId='+id,function(data){
			imgEle.parents("li").remove();
		    });
		}
	    });
	    
	    $('#delete_cert').click(function(){
		
		var imgEle = $(this);
		var id = $("input[name=project_id]").val();
		var old_cert = $("input[name=old_cert]").val();
		
		if(confirm('Sure?'))
		{
		    $.post('del_cert.php','id='+id+'&old_cert='+old_cert,function(data){
			imgEle.parent("div").remove();
		    });
		}
	    });
	    
	    $("#list1").dragsort({ dragSelector: "div", dragBetween: true, dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });
	});
	
	function saveOrder() {
	    var serialStr = "";
	    $("#list1 li span").each(function(i, elm) {
		serialStr += (i > 0 ? "|" : "") + $(elm).children("p").html();
	    });
	    
	    $("input[name=list1SortOrder]").val(serialStr);
	};
        
        function uploadify_(){
          if(queueSize!=0){
            $('#file_upload').uploadifyUpload();
          }else{
            window.location='index.php';
            //window.location='edit.php?aid=<?php echo $project_id; ?>';
          }
        }
        
         var queueSize = 0;
  
      $('#file_upload').uploadify({
        'uploader'    : '../js/jquery.uploadify/uploadify.swf',
        'script'      : 'update_uploadify.php',
        'cancelImg'   : '../js/jquery.uploadify/cancel.png',
        'fileDataName':'projectfiles',
        'scriptData'  : {'pid':<?php echo $project_id; ?>},
        'fileExt'        : '*.jpg;*.gif;*.png',
        'fileDesc'       : 'Image Files (.JPG, .GIF, .PNG)',
        'buttonText'  : 'Browse...',
        'multi'       : true,
        onCancel: function (a, b, c, d) {
            queueSize = d.fileCount;
        },
        onClearQueue: function (a, b) {
            queueSize = b.fileCount;
        },
        onSelectOnce: function (a, b) {
            queueSize = b.fileCount;
        },
        onAllComplete: function () {      
            queueSize = 0;
        },
        onComplete: function (a, b ,c, d, e) {
            queueSize = 0;
        },
        'onAllComplete' : function(e,d){
          if(d==0)
          {
            alert('Image(s) can\'t be uploaded, please check and edit again later.');
          }
          setInterval("window.location='index.php';",1000);
          //setInterval("window.location='edit.php?aid=<?php echo $project_id; ?>';",1000);
        }
      });
    </script>
<? include ("../footer.php") ?>